﻿ko.observableArray.fn.sortByProperty = function (prop) {
    this.sort(function (obj1, obj2) {
        var obj1PropValue = ko.isObservable(obj1[prop]) ? ko.utils.unwrapObservable(obj1[prop]) : obj1[prop];
        var obj2PropValue = ko.isObservable(obj2[prop]) ? ko.utils.unwrapObservable(obj2[prop]) : obj2[prop];

        if (obj1PropValue == obj2PropValue)
            return 0;
        else if (obj1PropValue < obj2PropValue)
            return -1;
        else
            return 1;
    });
};

ko.observableArray.fn.sortByPropertyInReverse = function (prop) {
    this.sort(function (obj1, obj2) {
        var obj1PropValue = ko.isObservable(obj1[prop]) ? ko.utils.unwrapObservable(obj1[prop]) : obj1[prop];
        var obj2PropValue = ko.isObservable(obj2[prop]) ? ko.utils.unwrapObservable(obj2[prop]) : obj2[prop];

        if (obj1PropValue == obj2PropValue)
            return 0;
        else if (obj1PropValue < obj2PropValue)
            return 1;
        else
            return -1;
    });
};